package com.pi.shisan.service.service.impl.next;

import com.pi.shisan.service.annotation.CalcNextTimeType;
import com.pi.shisan.service.constants.ClockTypeEnum;
import com.pi.shisan.service.domain.LunarMapping;
import com.pi.shisan.service.dto.ClockInfoDTO;
import com.pi.shisan.service.mapper.LunarMappingMapper;
import com.pi.shisan.service.service.ClockNextTimeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;

@CalcNextTimeType(ClockTypeEnum.LUNAR_BIRTHDAY)
public class LunarBirthdayNextTimeServiceImpl implements ClockNextTimeService {

    @Autowired
    private LunarMappingMapper lunarMappingMapper;

    @Override
    public Long calc(ClockInfoDTO clockInfoDTO) {
        LocalDateTime date = LocalDateTime.now();
        if (date.getHour() * 60 + date.getMinute() >= clockInfoDTO.getClkHour() * 60 + clockInfoDTO.getClkMinute()) {
            date = date.plusDays(1);
        }
        int dayint = date.getYear() * 10000 + date.getMonthValue() * 100 + date.getDayOfMonth();
        LunarMapping lunarMapping = lunarMappingMapper.selectNext(clockInfoDTO.getClkMonth() * 100 + clockInfoDTO.getClkDay(), dayint);
        if (lunarMapping != null) {
            dayint = lunarMapping.getDayint();
            date = LocalDateTime.of(
                    dayint / 10000,
                    dayint /100  % 100,
                    dayint % 100,
                    clockInfoDTO.getClkHour(),
                    clockInfoDTO.getClkMinute(),
                    0
            );
            return date.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        }
        return null;
    }
}
